package com.ruoyi.project.erp.setupbills.controller;

import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.project.erp.saleservices.domain.Saleservices;
import com.ruoyi.project.erp.saleservices.service.ISaleservicesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.erp.setupbills.domain.Setupbills;
import com.ruoyi.project.erp.setupbills.service.ISetupbillsService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2020-08-31
 */
@Controller
@RequestMapping("/erp/setupbills")
public class SetupbillsController extends BaseController
{
    private String prefix = "erp/setupbills";

    @Autowired
    private ISetupbillsService setupbillsService;
    @Autowired
    private ISaleservicesService saleservicesService;

    @RequiresPermissions("erp:setupbills:view")
    @GetMapping()
    public String setupbills()
    {
        return prefix + "/setupbills";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("erp:setupbills:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Setupbills setupbills)
    {
      /*  startPage();
        List<Setupbills> list = setupbillsService.selectSetupbillsList(setupbills);
        return getDataTable(list);*/

        Integer pageNum = ServletUtils.getParameterToInt(Constants.PAGE_NUM);
        Integer pageSize =ServletUtils.getParameterToInt(Constants.PAGE_SIZE);
        List<Setupbills> list = setupbillsService.selectSetupbillsList(setupbills);
        List<Setupbills> resultList = new ArrayList<>();
        List<Setupbills> resultQueryList = new ArrayList<>();
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        int totalPage = list.size()/pageSize+1;
        int count = pageNum*pageSize;
        if (pageNum == totalPage){
            count = list.size();
        }
        for(int start = (pageNum-1)*pageSize;start<count;start++){
        //for(int start = 0;start<list.size();start++){
            Setupbills temp = list.get(start);
            /*Saleservices saleservices = new Saleservices();
            saleservices.setMid(temp.getId());
            List<Saleservices> saleservicesList = saleservicesService.selectSaleservicesList(saleservices);
            temp.setSaleserviceTimes(String.valueOf(saleservicesList.size()));
            temp.setIsChangeDeal("否");
            temp.setIsChangeProduct("否");
            for(int i=0;i<saleservicesList.size();i++){
                if("换货".equals(saleservicesList.get(i).getCtype())){
                    temp.setIsChangeProduct("是");
                    if("未处理".equals(saleservicesList.get(i).getSt())){
                        temp.setIsChangeDeal("是");
                    }
                }
            }
            if(EmptyUtils.isNotEmpty(setupbills.getSaleserviceTimes())){
                if(saleservicesList.size() >=Integer.parseInt(setupbills.getSaleserviceTimes()) && saleservicesList.size() <=Integer.parseInt(setupbills.getEsaleserviceTimes())){
                    resultList.add(list.get(start));
                }
            }else{
                resultList.add(list.get(start));
            }*/
            resultList.add(list.get(start));
        }
        rspData.setRows(resultList);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
        /*if("是".equals(setupbills.getIsChangeDeal())){
            if("是".equals(setupbills.getIsChangeProduct())){
                for(int m=0;m<resultList.size();m++){
                    if("是".equals(resultList.get(m).getIsChangeDeal()) && "是".equals(resultList.get(m).getIsChangeProduct()) ){
                        resultQueryList.add(resultList.get(m));
                    }
                }
            }else if("否".equals(setupbills.getIsChangeProduct())){
                for(int m=0;m<resultList.size();m++){
                    if("是".equals(resultList.get(m).getIsChangeDeal()) && "否".equals(resultList.get(m).getIsChangeProduct()) ){
                        resultQueryList.add(resultList.get(m));
                    }
                }
            }else{
                for(int m=0;m<resultList.size();m++){
                    if("是".equals(resultList.get(m).getIsChangeDeal())){
                        resultQueryList.add(resultList.get(m));
                    }
                }
            }
            *//*for(int start = (pageNum-1)*pageSize;start<count;start++){
                returnQueryList.add(resultQueryList.get(start));
            }*//*
            rspData.setRows(resultQueryList);
            rspData.setTotal(new PageInfo(list).getTotal());
            return rspData;
        }else if("否".equals(setupbills.getIsChangeDeal())){
            if("是".equals(setupbills.getIsChangeProduct())){
                for(int m=0;m<resultList.size();m++){
                    if("否".equals(resultList.get(m).getIsChangeDeal()) && "是".equals(resultList.get(m).getIsChangeProduct()) ){
                        resultQueryList.add(resultList.get(m));
                    }
                }
            }else if("否".equals(setupbills.getIsChangeProduct())){
                for(int m=0;m<resultList.size();m++){
                    if("否".equals(resultList.get(m).getIsChangeDeal()) && "否".equals(resultList.get(m).getIsChangeProduct()) ){
                        resultQueryList.add(resultList.get(m));
                    }
                }
            }else{
                for(int m=0;m<resultList.size();m++){
                    if("否".equals(resultList.get(m).getIsChangeDeal())){
                        resultQueryList.add(resultList.get(m));
                    }
                }
            }
            *//*for(int start = (pageNum-1)*pageSize;start<count;start++){
                returnQueryList.add(resultQueryList.get(start));
            }*//*
            rspData.setRows(resultQueryList);
            rspData.setTotal(new PageInfo(list).getTotal());
            return rspData;
        }else{
            if("是".equals(setupbills.getIsChangeProduct())){
                for(int m=0;m<resultList.size();m++){
                    if("是".equals(resultList.get(m).getIsChangeProduct()) ){
                        resultQueryList.add(resultList.get(m));
                    }
                }
            }else if("否".equals(setupbills.getIsChangeProduct())){
                for(int m=0;m<resultList.size();m++){
                    if("否".equals(resultList.get(m).getIsChangeProduct()) ){
                        resultQueryList.add(resultList.get(m));
                    }
                }
            }else{
                for(int m=0;m<resultList.size();m++){
                    resultQueryList.add(resultList.get(m));
                }
            }
            *//*for(int start = (pageNum-1)*pageSize;start<count;start++){
                returnQueryList.add(resultQueryList.get(start));
            }*/
           /* rspData.setRows(resultQueryList);
            rspData.setTotal(new PageInfo(list).getTotal());*/
           /* return rspData;
        }*/

    }


    /**
     * 手机端查询列表
     */
    @PostMapping("/listPhone")
    @ResponseBody
    public AjaxResult listPhone(@RequestBody Setupbills setupbills)
    {
        List<Setupbills> list = setupbillsService.selectSetupbillsList(setupbills);
        return new AjaxResult(AjaxResult.Type.SUCCESS,"200",list);
    }


    /**
     * PC查询售后记录
     */
    @RequiresPermissions("erp:setupbills:list")
    @PostMapping("/listSaleService")
    @ResponseBody
    public TableDataInfo listSaleService(long mid)
    {
        Saleservices saleservices = new Saleservices();
        saleservices.setMid(mid);
        List<Saleservices> childList =saleservicesService.selectSaleservicesList(saleservices);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(childList);
        return rspData;
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("erp:setupbills:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Setupbills setupbills)
    {
        List<Setupbills> list = setupbillsService.selectSetupbillsList(setupbills);
        ExcelUtil<Setupbills> util = new ExcelUtil<Setupbills>(Setupbills.class);
        return util.exportExcel(list, "setupbills");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    //@RequiresPermissions("erp:setupbills:add")
    //@Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody Setupbills setupbills)
    {
        return toAjax(setupbillsService.insertSetupbills(setupbills));
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Setupbills setupbills = setupbillsService.selectSetupbillsById(id);
        mmap.put("setupbills", setupbills);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("erp:setupbills:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Setupbills setupbills)
    {
        return toAjax(setupbillsService.updateSetupbills(setupbills));
    }

    /**
     * 删除【请填写功能名称】
     */
    //@RequiresPermissions("erp:setupbills:remove")
    //@Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(setupbillsService.deleteSetupbillsByIds(ids));
    }
}
